/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.utils.middleware;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import org.orm.PersistentException;
import org.orm.PersistentTransaction;
import org.testis.orm.classes.T_bed_testpc;
import org.testis.orm.classes.T_bed_testpcCriteria;
import org.testis.orm.classes.T_bed_testpcFactory;

/**
 * 
 * @author selcuk
 */
public class TestPcDataHandler extends AtofOrmFlyweightFactory {

    private static TestPcDataHandler m_reference;

    private TestPcDataHandler() {
	loadTestPCs();
    }

    public static TestPcDataHandler newInstance() {
	if (m_reference == null) {
	    m_reference = new TestPcDataHandler();
	}
	return m_reference;
    }

    public Collection getTestPCs() {
	return FLY_WEIGHT_DATA.values();
    }

    public T_bed_testpc[] getTestPcArray() {
	Collection coll = getTestPCs();
	T_bed_testpc[] result = new T_bed_testpc[coll.size()];
	coll.toArray(result);
	return result;
    }

    public LinkedList<T_bed_testpc> getTestPcList() {
	Collection col = getTestPCs();
	return new LinkedList<T_bed_testpc>(col);
    }

    public HashMap getTestPcMap() {
	return new HashMap(FLY_WEIGHT_DATA);
    }

    public int getNumberOfTestPCs() {
	return FLY_WEIGHT_DATA.size();
    }

    public T_bed_testpc getTestPcById(int id) {
	T_bed_testpc result = null;
	Object obj = getFlyweight(Integer.valueOf(id));
	if (obj != null) {
	    result = (T_bed_testpc) obj;
	}
	return result;
    }

    public int addTestPc(String name) {
	T_bed_testpc testpc = createTestpc(name);
	int id = 0;
	if (testpc != null) {
	    id = testpc.getId();
	    putFlyweight(Integer.valueOf(id), testpc);
	}
	return id;
    }

    public boolean saveTestPc(int id, String name) {
	return updateTestPc(id, name);
    }

    @Override
    protected Object loadFlyweight(Object key) {
	T_bed_testpc obj = readTestPcById((Integer) key);
	return obj;
    }

    private void loadTestPCs() {
	T_bed_testpc[] testPCs = readTestPCs();
	if (testPCs != null) {
	    for (int i = 0; i < testPCs.length; i++) {
		putFlyweight(Integer.valueOf(testPCs[i].getId()), testPCs[i]);
	    }
	}
    }

    private T_bed_testpc readTestPcById(int id) {
	T_bed_testpc result = null;
	try {
	    T_bed_testpcCriteria criteria = new T_bed_testpcCriteria();
	    criteria.id.eq(id);
	    result = criteria.uniqueT_bed_testpc();
	} catch (PersistentException ex) {
	}
	return result;
    }

    private T_bed_testpc[] readTestPCs() {
	T_bed_testpc[] result = null;
	try {
	    result = T_bed_testpcFactory.listT_bed_testpcByQuery(null, null);
	    if (result == null) {
		result = new T_bed_testpc[0];
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }

    private T_bed_testpc createTestpc(String name) {
	T_bed_testpc result = null;
	try {
	    PersistentTransaction t = openDBConnection().beginTransaction();
	    try {
		T_bed_testpc obj = new T_bed_testpc();
		obj.setName(name);
		obj.save();
		t.commit();
		result = obj;
	    } catch (PersistentException ex) {
		t.rollback();
	    } finally {
		closeDBConnection();
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }

    private boolean updateTestPc(int id, String name) {
	boolean result = false;
	try {
	    PersistentTransaction t = openDBConnection().beginTransaction();
	    try {
		T_bed_testpc obj = getTestPcById(id);
		if (obj != null) {
		    obj.setName(name);
		    result = obj.save();
		}
		t.commit();
	    } catch (PersistentException ex) {
		t.rollback();
	    } finally {
		closeDBConnection();
	    }
	} catch (PersistentException ex) {
	}
	return result;
    }
}
